/**
 * Created by sunyxq on 16/3/26.
 */
angular.module('hyt.hzYunDanCtrl',[])

    .controller('HzCydListCtrl',function($scope,HzCydListFactory,$state,ENV,Storage,$stateParams,$ionicPopup,submitFhdFactory,$ionicLoading,receiptFactory){
        $scope.showloading=true;
        var sid=Storage.get('sid');
        var user_id=Storage.get('user_id');
        var yh_id=Storage.get('yh_id');
        var lxdh=Storage.get('lxdh');
        var cydh=$stateParams.cydh;
        var kssj=$stateParams.kssj;
        var jssj=$stateParams.jssj;
        var shrdh=$stateParams.shrdh;
        var shrmc=$stateParams.shrmc;
        var fhdzt=$stateParams.fhdzt;
        var minCyzt='',maxCyzt='';
        Storage.set('buDan',null);
        Storage.set('fhd',null);
        if (fhdzt==3){
          minCyzt=10;
          maxCyzt=70;
        }else if (fhdzt==5){
          minCyzt=70;
          maxCyzt=100;
        }else if(fhdzt==6){
          minCyzt=100;
          maxCyzt=150;
        }
        HzCydListFactory.getFhdList(sid,user_id,yh_id,lxdh,cydh,kssj,jssj,shrdh,shrmc,fhdzt,minCyzt,maxCyzt);
        HzCydListFactory.getShdList(sid,user_id,yh_id,lxdh,cydh,kssj,jssj,shrdh,shrmc,fhdzt,minCyzt,maxCyzt);
        $scope.$on('FhdList.fhdUpdated', function() {
            var dataFactory=HzCydListFactory.getFhd();
            $scope.fhdData=dataFactory.data;
            console.log($scope.fhdData);
            $scope.showloading=false;

        });
        $scope.$on('ShdList.shdUpdated', function() {
            var dataFactory=HzCydListFactory.getShd();
            $scope.shdData=dataFactory.data;
            console.log($scope.shdData);
            $scope.showloading=false;
        });
        //下拉更新
        $scope.doRefreshF=function(){
            HzCydListFactory.getFhdList(sid,user_id,yh_id,lxdh,cydh,kssj,jssj,shrdh,shrmc,fhdzt,minCyzt,maxCyzt);
            $scope.$broadcast('scroll.refreshComplete');
        };

        //上拉更新
        $scope.loadMoreF=function(){
            HzCydListFactory.getMoreFhd(sid,user_id,yh_id,lxdh,cydh,kssj,jssj,shrdh,shrmc,fhdzt,minCyzt,maxCyzt);
            $scope.$broadcast('scroll.infiniteScrollComplete');
        };
        //下一页
        $scope.hasNextPageF = function() {
            return HzCydListFactory.hasNextPageFhd();
        };
        //下拉更新
        $scope.doRefreshS=function(){
            HzCydListFactory.getShdList(sid,user_id,yh_id,lxdh,cydh,kssj,jssj,shrdh,shrmc,fhdzt,minCyzt,maxCyzt);
            $scope.$broadcast('scroll.refreshComplete');
        };
        $scope.selectTab=function(){
            HzCydListFactory.getShdList(sid,user_id,yh_id,lxdh,cydh,kssj,jssj,shrdh,shrmc,fhdzt,minCyzt,maxCyzt);
        };
        //上拉更新
        $scope.loadMoreS=function(){
            HzCydListFactory.getMoreShd(sid,user_id,yh_id,lxdh,cydh,kssj,jssj,shrdh,shrmc,fhdzt,minCyzt,maxCyzt);
            $scope.$broadcast('scroll.infiniteScrollComplete');
        };
        //下一页
        $scope.hasNextPageS = function() {
            return HzCydListFactory.hasNextPageShd();
        };
        $scope.getFhdZt=function(zt){
            switch (zt){
                case 1:
                    $scope.fhdzt = "待提交";
                    break;
                case 2:
                    $scope.fhdzt = "待承运";
                    break;
                case 3:
                    $scope.fhdzt = "已承运";
                    break;
                case 4:
                    $scope.fhdzt = "拒承运";
                    break;
                case 5:
                    $scope.fhdzt = "已到站";
                    break;
                case 6:
                    $scope.fhdzt = "已签收";
                    break;
                default:
                    break;
            }
            return $scope.fhdzt;
        };
        $scope.getCydZt=function(zt) {
            if (zt < 70) {
                $scope.cyzt = '已承运';
            } else if (zt >= 70 && zt < 100) {
                $scope.cyzt = '已到站';
            } else if (zt >= 100) {
                $scope.cyzt = '已签收';
            }
            return $scope.cyzt;
        }
        //提交发货单
        $scope.submitFhd=function(fhd_id,fhrdh,fcity){
            $state.go('company',{fhd_id:fhd_id,fhrdh:fhrdh,fhrCity:fcity});
        };
        //撤回发货单
        $scope.recall=function(fhd_id,dd_id){
            var receipt = $ionicPopup.confirm({
                title : '是否撤回发货单',
                cancelText : '否',
                cancelType : 'button-stable',
                okText : '是'
            });
            receipt.then(function(r){
                if(r){
                    submitFhdFactory.getRecallFhd(sid,user_id,yh_id,lxdh,fhd_id,dd_id);
                    $scope.$on('Recall.fhdUpdated',function(){
                        var result=submitFhdFactory.getResultRecall();
                        if (result==1){
                            $ionicLoading.show({
                                noBackdrop: true,
                                template: "操作成功",
                                duration: 1500
                            });
                            HzCydListFactory.getFhdList(sid,user_id,yh_id,lxdh,cydh,kssj,jssj,shrdh);
                        }
                    });
                }else{
                    console.log('取消');
                }
            });
        };
        //修改
        $scope.fhdChange=function(fhd_id,fhdlx){
            if (fhdlx==1){
                $state.go('hzChangeDan',{fhd_id:fhd_id});
            }else if (fhdlx==3){
                $state.go('changeBuDan',{fhd_id:fhd_id});
            }
        };
        //签收
        $scope.doReceipt = function(shd){
            var cyd_id=shd.cyd_id;
            var receipt = $ionicPopup.confirm({
                title : '确认签收',
                cancelText : '取消',
                cancelType : 'button-stable',
                okText : '确定'
            });
            receipt.then(function(r){
                if(r){
                    receiptFactory.qianshou(sid,user_id,yh_id,cyd_id,lxdh);
                    $scope.$on('Hz_qsUpdated',function(){
                        var result=receiptFactory.getQsResult();
                        if(result=='000000'){
                            $ionicLoading.show({
                                noBackdrop: true,
                                template: "签收成功！",
                                duration: 600
                            });
                            HzCydListFactory.getShdList(sid,user_id,yh_id,lxdh,cydh,kssj,jssj,shrdh);
                        }else {
                            $ionicLoading.show({
                                noBackdrop: true,
                                template: "签收失败！",
                                duration: 600
                            });
                        }
                    });

                }else{
                    console.log('取消');
                }
            });
        };
        $scope.hzFhdDetail=function(fhd_id,fhdlx,cyd_id){
            if (fhdlx==2||cyd_id!=''){
                $state.go('hzcydtab',{fhd_id:fhd_id,cyd_id:cyd_id});
            }else {
                $state.go('hzFhdDetail',{fhd_id:fhd_id});
            }
        };
        $scope.deleteFhd=function(fhd){
            if(fhd.fhdlx==3||fhd.fhdzt==1){
                var receipt = $ionicPopup.confirm({
                    title : '是否删除发货单',
                    cancelText : '否',
                    cancelType : 'button-stable',
                    okText : '是'
                });
                receipt.then(function(r){
                    if(r){
                        HzCydListFactory.getDeleteFhd(fhd.fhd_id,sid,user_id,yh_id);
                        $scope.$on('Delete.fhdUpdated',function(){
                            var result=HzCydListFactory.getResult();
                            if(result==1){
                                $ionicLoading.show({
                                    noBackdrop: true,
                                    template: "删除成功！",
                                    duration: 600
                                });
                                HzCydListFactory.getFhdList(sid,user_id,yh_id,lxdh,cydh,kssj,jssj,shrdh);
                            }else {
                                $ionicLoading.show({
                                    noBackdrop: true,
                                    template: "删除失败！",
                                    duration: 600
                                });
                            }
                        });

                    }else{
                        console.log('取消');
                    }
                });
            }else {
                $ionicLoading.show({
                    noBackdrop: true,
                    template: "无法删除此类发货单！",
                    duration: 600
                });
            }
        };
    })
    //修改发货单
    .controller('HzChangeDanCtrl',function($scope,$state,ENV,Storage,HzCydDetailFactory,$stateParams,$timeout,$filter,FProCitAreFactory,SProCitAreFactory,AddCydFactory,changeFhdFactory,$ionicLoading){
        var sid=Storage.get("sid");
        var yh_id=Storage.get("yh_id");
        var user_id=Storage.get('user_id');
        var lxdh=Storage.get('lxdh');
        var id=$stateParams.fhd_id;
        var changeDqBsF=false;
        var changeDqBsS=false;
        $scope.$on('FhdDetail.detailUpdated',function(){
            var fhdDetailData= HzCydDetailFactory.getDetailContent();
            $scope.fhd=fhdDetailData;
            if ($scope.fhd.tbzt==1){
                $scope.fhd.tbzt=true;
            }
            if ($scope.fhd.sfhd==1){
                $scope.fhd.sfhd=true;
            }
            FProCitAreFactory.getCity(sid,user_id,yh_id,$scope.fhd.fprocode);
            SProCitAreFactory.getCity(sid,user_id,yh_id,$scope.fhd.sprocode);
            FProCitAreFactory.getAre(sid,user_id,yh_id,$scope.fhd.fcitycode);
            SProCitAreFactory.getAre(sid,user_id,yh_id,$scope.fhd.scitycode);
            $timeout(function(){
              $('#shType').val($scope.fhd.shfs);
              $('#provinceF').val($scope.fhd.fprocode);
              $('#cityF').val($scope.fhd.fcitycode);
              $('#areaF').val($scope.fhd.fareacode);
              $('#provinceS').val($scope.fhd.sprocode);
              $('#cityS').val($scope.fhd.scitycode);
              $('#areaS').val($scope.fhd.sareacode);
            },500);
        });
        if (Storage.get('fhd')!=null){
            $scope.fhd=Storage.get('fhd');
            $('.bjsmDot').val($scope.fhd.bjsm);
            $('.dshkDot').val($scope.fhd.dshk);
            $('.zlDot').val($scope.fhd.zl);
            $('.tjDot').val($scope.fhd.tj);
            if (Storage.get('provinceF')!='地区选择'){
                $scope.citiesF=Storage.get('citiesDataF');
                $scope.areasF=Storage.get('areasDataF');
                $timeout(function(){
                    $('#provinceF').val(Storage.get('provinceF'));
                    $('#cityF').val(Storage.get('cityF'));
                    $('#areaF').val(Storage.get('areaF'));
                },300);
            }
            if (Storage.get('provinceS')!='地区选择'){
                $scope.citiesS=Storage.get('citiesDataS');
                $scope.areasS=Storage.get('areasDataS');
                $timeout(function(){
                    $('#provinceS').val(Storage.get('provinceS'));
                    $('#cityS').val(Storage.get('cityS'));
                    $('#areaS').val(Storage.get('areaS'));
                },300);
            }
            $timeout(function(){
                $('#shType').val(Storage.get('hzShfs'));
                if ($stateParams.khmc!=''||$stateParams.lxdh!=''){
                    $scope.fhd.shrmc=$stateParams.khmc;
                    $scope.fhd.shrdh=$stateParams.lxdh;
                    $scope.fhd.shrdz=$stateParams.address;
                }
            },200);
        }else {
            HzCydDetailFactory.getDetail(id,sid,user_id,yh_id);
        }
        //发货日期
        $scope.showStartDate = function() { //选择日期
            var optionsStart = {
                date:new Date($scope.fhd.fhrq),
                mode: 'date'
            };
            datePicker.show(optionsStart, function(d) {
                if (!isNaN(d.getTime())) {  // valid date
                    $scope.$apply(function () {
                        $scope.fhd.fhrq = $filter('date')(d,'yyyy-MM-dd');
                    });
                }
            });
        };
        if (Storage.get('provincesData')!=null){
          $scope.provinces=Storage.get('provincesData');
        }else {
          FProCitAreFactory.getPro(sid,user_id,yh_id);
        }
        $scope.$on('FPro.proUpdated',function(){
            $scope.provinces=FProCitAreFactory.getP();
            Storage.set('provincesData',$scope.provinces);
        });
        $scope.$on('FCity.cityUpdated',function(){
            $scope.citiesF=FProCitAreFactory.getC();
            if (changeDqBsF==true){
              FProCitAreFactory.getAre(sid,user_id,yh_id,$scope.citiesF[0].code);
            }
            Storage.set('citiesDataF',$scope.citiesF);
        });
        $scope.$on('FArea.areaUpdated',function(){
            $scope.areasF=FProCitAreFactory.getA();
            Storage.set('areasDataF',$scope.areasF);
        });
        $scope.$on('SCity.cityUpdated',function(){
            $scope.citiesS=SProCitAreFactory.getC();
            if (changeDqBsS==true){
              SProCitAreFactory.getAre(sid,user_id,yh_id,$scope.citiesS[0].code);
            }
            Storage.set('citiesDataS',$scope.citiesS);
        });
        $scope.$on('SArea.areaUpdated',function(){
            $scope.areasS=SProCitAreFactory.getA();
            Storage.set('areasDataS',$scope.areasS);
        });
        $("#provinceF").change(function(e) {
            changeDqBsF=true;
            FProCitAreFactory.getCity(sid,user_id,yh_id,$('#provinceF').val());
            if ($('#provinceF').val()=='地区选择'){
                $timeout(function(){
                    $scope.citiesF=null;
                    $scope.areasF=null;
                },300);
            }
        });
        $("#cityF").change(function(e) {
            FProCitAreFactory.getAre(sid,user_id,yh_id,$('#cityF').val());
        });
        $("#provinceS").change(function(e) {
            changeDqBsS=true;
            SProCitAreFactory.getCity(sid,user_id,yh_id,$('#provinceS').val());
            if ($('#provinceS').val()=='地区选择'){
                $timeout(function(){
                    $scope.citiesS=null;
                    $scope.areasS=null;
                },300);
            }
        });
        $("#cityS").change(function(e) {
            SProCitAreFactory.getAre(sid,user_id,yh_id,$('#cityS').val());
        });
        AddCydFactory.queryShfs(sid,yh_id,user_id);
        $scope.$on('InitData',function(){
            $scope.shfs=AddCydFactory.getShfs();
        });
        $(".zlDot").on("blur",function(){
            $(this).val(parseFloat($(this).val()).toFixed(3));
            $scope.fhd.zl=$(this).val();
            if($scope.fhd.zl>3000000){
                $ionicLoading.show({
                  noBackdrop: true,
                  template: "重量不可超过3000000吨",
                  duration: 1500
                });
                $("#zl").css("color","red");
            }else{
                $("#zl").css("color","black");
            }
        });
        $(".tjDot").on("blur",function(){
            $(this).val(parseFloat($(this).val()).toFixed(3));
            $scope.fhd.tj=$(this).val();
            if($scope.fhd.tj>3000000){
                $ionicLoading.show({
                  noBackdrop: true,
                  template: "体积不可超过3000000立方米",
                  duration: 1500
                });
                $("#tj").css("color","red");
            }else{
                $("#tj").css("color","black");
            }
        });
        $(".bjsmDot").on("blur",function(){
            $(this).val(parseFloat($(this).val()).toFixed(2));
            $scope.fhd.bjsm=$(this).val();
            if($scope.fhd.bjsm>3000000){
                $ionicLoading.show({
                  noBackdrop: true,
                  template: "费用金额不可超过3000000元",
                  duration: 1500
                });
                $(".bjsmDot").css("color","red");
            }else{
                $(".bjsmDot").css("color","black");
            }
        });
        $(".dshkDot").on("blur",function(){
            $(this).val(parseFloat($(this).val()).toFixed(2));
            $scope.fhd.dshk=$(this).val();
            if($scope.fhd.dshk>3000000){
                $ionicLoading.show({
                  noBackdrop: true,
                  template: "费用金额不可超过3000000元",
                  duration: 1500
                });
                $(".dshkDot").css("color","red");
            }else{
                $(".dshkDot").css("color","black");
            }
        });
        $scope.chooseShr=function(htmlbs){
            Storage.set('provinceF',$('#provinceF option:selected').val());
            Storage.set('cityF',$('#cityF option:selected').val());
            Storage.set('areaF',$('#areaF option:selected').val());
            Storage.set('provinceS',$('#provinceS option:selected').val());
            Storage.set('cityS',$('#cityS option:selected').val());
            Storage.set('areaS',$('#areaS option:selected').val());
            Storage.set('hzShfs',$('#shType option:selected').val());
            $timeout(function(){
                Storage.set('fhd',$scope.fhd);
            },160);
            $state.go('hzShrList',{htmlbs:htmlbs,fhd_id:id});
        };
        $scope.changejian=function(a){
            if(a.toString().length>=7){
                $ionicLoading.show({
                  noBackdrop: true,
                  template: "件数最大限制7位数",
                  duration: 1500
                });
            }
        };
        //保存发货单
        $scope.saveFhd=function(){
            var fproCode=$('#provinceF').val();
            var fcityCode=$('#cityF').val();
            var fareaCode=$('#areaF').val();
            var sproCode=$('#provinceS').val();
            var scityCode=$('#cityS').val();
            var sareaCode=$('#areaS').val();
            var fpro=$('#provinceF option:selected').text();
            var fcity=$('#cityF option:selected').text();
            var farea=$('#areaF option:selected').text();
            var spro=$('#provinceS option:selected').text();
            var scity=$('#cityS option:selected').text();
            var sarea=$('#areaS option:selected').text();
            var shfs=$('#shType option:selected').val();
            $("#save").attr("disabled", true);
            Storage.set('fhd',null);
            if ($('#provinceF option:selected').text()=='地区选择'){
                $ionicLoading.show({
                    noBackdrop: true,
                    template: "请选择发货地区",
                    duration: 600
                });
                $("#save").attr("disabled", false);
            }
            else if ($('#provinceS option:selected').text()=='地区选择'){
                $ionicLoading.show({
                    noBackdrop: true,
                    template: "请选择收货地区",
                    duration: 600
                });
                $("#save").attr("disabled", false);
            }else {
                $timeout(function(){
                    changeFhdFactory.getChangeFhd(id,sid,user_id,yh_id,$scope.fhd,fpro,fcity,farea,spro,scity,sarea,fproCode,fcityCode,fareaCode,sproCode,scityCode,sareaCode,shfs,1,1);
                },160);
                $scope.$on('Change.fhdUpdated',function(){
                    var result=changeFhdFactory.getResult();
                    if (result==1){
                        $ionicLoading.show({
                            noBackdrop: true,
                            template: "修改成功",
                            duration: 600
                        });
                        $("#save").attr("disabled", false);
                        $state.go('hzcydlist');
                    }
                });
            }
        };
        //提交发货单
        $scope.submitFhd=function(){
            var fproCode=$('#provinceF').val();
            var fcityCode=$('#cityF').val();
            var fareaCode=$('#areaF').val();
            var sproCode=$('#provinceS').val();
            var scityCode=$('#cityS').val();
            var sareaCode=$('#areaS').val();
            var fpro=$('#provinceF option:selected').text();
            var fcity=$('#cityF option:selected').text();
            var farea=$('#areaF option:selected').text();
            var spro=$('#provinceS option:selected').text();
            var scity=$('#cityS option:selected').text();
            var sarea=$('#areaS option:selected').text();
            var shfs=$('#shType option:selected').val();
            var fhrCity=$('#cityF option:selected').text();
            $("#fahuo").attr("disabled", true);
            Storage.set('fhd',null);
            if ($('#provinceF option:selected').text()=='地区选择'){
                $ionicLoading.show({
                    noBackdrop: true,
                    template: "请选择发货地区",
                    duration: 600
                });
                $("#fahuo").attr("disabled", false);
            }
            else if ($('#provinceS option:selected').text()=='地区选择'){
                $ionicLoading.show({
                    noBackdrop: true,
                    template: "请选择收货地区",
                    duration: 600
                });
                $("#fahuo").attr("disabled", false);
            }else {
                $timeout(function(){
                    changeFhdFactory.getChangeFhd(id,sid,user_id,yh_id,$scope.fhd,fpro,fcity,farea,spro,scity,sarea,fproCode,fcityCode,fareaCode,sproCode,scityCode,sareaCode,shfs,1,1);
                },160);
                $scope.$on('Change.fhdUpdated',function(){
                    var result=changeFhdFactory.getResult();
                    if (result==1){
                        $("#fahuo").attr("disabled", false);
                        $state.go('company',{fhd_id:id,fhrdh:$scope.fhd.fhrdh,fhrCity:fhrCity});
                    }
                });
            }
        };
    })
    //修改补单
    .controller('ChangeBuDanCtrl',function($scope,$state,ENV,Storage,$stateParams,$timeout,$filter,AddCydFactory,$ionicPopup,FProCitAreFactory,SProCitAreFactory,$ionicLoading,changeBdFactory,HzCydDetailFactory,$ionicActionSheet,$cordovaCamera,$cordovaImagePicker,saveBdFactory){
        var sid=Storage.get("sid");
        var yh_id=Storage.get("yh_id");
        var user_id=Storage.get('user_id');
        var lxdh=Storage.get('lxdh');
        var id=$stateParams.fhd_id;
        var changeDqBsF=false;
        var changeDqBsS=false;
        $scope.items=[];
        function addFyMenu(){
            if($scope.buDan.shf==''||$scope.buDan.shf==null||$scope.buDan.shf==0)
            {
              $scope.items.push({index:1,name:'送货费'});
            }
            if($scope.buDan.thf==''||$scope.buDan.thf==null||$scope.buDan.thf==0)
            {
              $scope.items.push({index:2,name:'提货费'});
            }
            if(($scope.buDan.bjsm==''||$scope.buDan.bjsm==null||$scope.buDan.bjsm==0)&&($scope.buDan.bf==''||$scope.buDan.bf==null||$scope.buDan.bf==0))
            {
              $scope.items.push({index:3,name:'保额'});
              $scope.items.push({index:4,name:'保费'});
            }
            if($scope.buDan.yf_df==''||$scope.buDan.yf_df==null||$scope.buDan.yf_df==0)
            {
              $scope.items.push({index:5,name:'运费垫付'});
            }
            if($scope.buDan.dsk_df==''||$scope.buDan.dsk_df==null||$scope.buDan.dsk_df==0)
            {
              $scope.items.push({index:6,name:'代收款垫付'});
            }
        }
        //判断添加的费用是否有值
        function judgeYf(){
            if($scope.buDan.shf!=''&&$scope.buDan.shf!=null){
              $("#shfLab").removeClass("hide").addClass("showfy");
            }
            if($scope.buDan.thf!=''&&$scope.buDan.thf!=null){
              $("#thfLab").removeClass("hide").addClass("showfy");
            }
            if(($scope.buDan.bjsm!=''&&$scope.buDan.bjsm!=null)||$scope.buDan.tbzt=='1'){
              $("#beLab").removeClass("hide").addClass("showfy");
              $("#bfLab").removeClass("hide").addClass("showfy");
            }
            if($scope.buDan.bf!=''&&$scope.buDan.bf!=null){
              $("#beLab").removeClass("hide").addClass("showfy");
              $("#bfLab").removeClass("hide").addClass("showfy");
            }
            if($scope.buDan.yf_df!=''&&$scope.buDan.yf_df!=null){
              $("#yfdfLab").removeClass("hide").addClass("showfy");
            }
            if($scope.buDan.dsk_df!=''&&$scope.buDan.dsk_df!=null){
              $("#dskdfLab").removeClass("hide").addClass("showfy");
            }
        }
        $scope.tbzt= {
            isCk: ''
        };
        $scope.sfhd={
            isHd:''
        };
        $scope.$on('FhdDetail.detailUpdated',function(){
            var bdDetailData= HzCydDetailFactory.getDetailContent();
            $scope.buDan=bdDetailData;
            console.log($scope.buDan);
            if($scope.buDan.tbzt==''){
                $scope.tbzt.isCk=false;
            }else if($scope.buDan.tbzt=='1'){
                $scope.tbzt.isCk=true;
            }
            if($scope.buDan.sfhd==''){
                $scope.sfhd.isHd=false;
            }else if($scope.buDan.sfhd=='1'){
                $scope.sfhd.isHd=true;
            }
            if ($scope.buDan.fprocode!=0){
                FProCitAreFactory.getCity(sid,user_id,yh_id,$scope.buDan.fprocode);
                FProCitAreFactory.getAre(sid,user_id,yh_id,$scope.buDan.fcitycode);
            }else {
                $scope.buDan.fprocode='地区选择';
            }
            if ($scope.buDan.sprocode!=0){
                SProCitAreFactory.getCity(sid,user_id,yh_id,$scope.buDan.sprocode);
                SProCitAreFactory.getAre(sid,user_id,yh_id,$scope.buDan.scitycode);
            }else {
                $scope.buDan.sprocode='地区选择';
            }
            if($scope.buDan.yf_xf==0){
              $scope.buDan.yf_xf='';
            }
            if($scope.buDan.yf_tf==0){
              $scope.buDan.yf_tf='';
            }
            if($scope.buDan.yf_hf==0){
              $scope.buDan.yf_hf='';
            }
            if($scope.buDan.yf_yj==0){
              $scope.buDan.yf_yj='';
            }
            if($scope.buDan.yf_ycdk==0){
              $scope.buDan.yf_ycdk='';
            }
            if($scope.buDan.sl==0){
              $scope.buDan.sl='';
            }
            addFyMenu();
            judgeYf();
            $timeout(function(){
                $('#shfs').val($scope.buDan.shfs);
                $('#provincef').val($scope.buDan.fprocode);
                $('#cityf').val($scope.buDan.fcitycode);
                $('#areaf').val($scope.buDan.fareacode);
                $('#provinces').val($scope.buDan.sprocode);
                $('#citys').val($scope.buDan.scitycode);
                $('#areas').val($scope.buDan.sareacode);
            },500);
        });
        if (Storage.get('buDan')!=null){
            $scope.buDan=Storage.get('buDan');
            $(".zlDot").val($scope.buDan.zl);
            $(".tjDot").val($scope.buDan.tj);
            $(".numDot").val($scope.buDan.yf);
            $(".xfDot").val($scope.buDan.yf_xf);
            $(".tfDot").val($scope.buDan.yf_tf);
            $(".hfDot").val($scope.buDan.yf_hf);
            $(".yjDot").val($scope.buDan.yf_yj);
            $(".dkDot").val($scope.buDan.yf_ycdk);
            $(".dshkDot").val($scope.buDan.dshk);
            $(".xxfDot").val($scope.buDan.xxf_xf);
            $(".qfDot").val($scope.buDan.xxf_qf);
            $(".shfDot").val($scope.buDan.shf);
            $(".thfDot").val($scope.buDan.thf);
            $(".beDot").val($scope.buDan.bjsm);
            $(".bfDot").val($scope.buDan.bf);
            $(".yfdfDot").val($scope.buDan.yf_df);
            $(".dskdfDot").val($scope.buDan.dsk_df);
            $scope.imgDataB=Storage.get('imgData');
            addFyMenu();
            judgeYf();
            if (Storage.get('provincef')!='地区选择'){
                $scope.citiesF=Storage.get('citiesDataf');
                $scope.areasF=Storage.get('areasDataf');
                $timeout(function(){
                    $('#provincef').val(Storage.get('provincef'));
                    $('#cityf').val(Storage.get('cityf'));
                    $('#areaf').val(Storage.get('areaf'));
                },300);
            }
            if (Storage.get('provinces')!='地区选择'){
                $scope.citiesS=Storage.get('citiesDatas');
                $scope.areasS=Storage.get('areasDatas');
                $timeout(function(){
                    $('#provinces').val(Storage.get('provinces'));
                    $('#citys').val(Storage.get('citys'));
                    $('#areas').val(Storage.get('areas'));
                },300);
            }
            if($scope.buDan.tbzt==''){
              $scope.tbzt.isCk=false;
            }else if($scope.buDan.tbzt=='1'){
              $scope.tbzt.isCk=true;
            }
            if($scope.buDan.sfhd==''){
              $scope.sfhd.isHd=false;
            }else if($scope.buDan.sfhd=='1'){
              $scope.sfhd.isHd=true;
            }
            $timeout(function(){
                $('#shfs').val(Storage.get('bdShfs'));
                if ($stateParams.khmc!=''||$stateParams.lxdh!=''||$stateParams.address!=''){
                    $scope.buDan.shrmc=$stateParams.khmc;
                    $scope.buDan.shrdh=$stateParams.lxdh;
                    $scope.buDan.shrdz=$stateParams.address;
                }
            },200);
        }else {
            HzCydDetailFactory.getDetail(id,sid,user_id,yh_id);
        }
        //承运日期
        $scope.showStartDate = function() { //选择日期
            var optionsStart = {
                date:new Date($scope.buDan.cyrq),
                mode: 'date'
            };
            datePicker.show(optionsStart, function(d) {
                if (!isNaN(d.getTime())) {  // valid date
                    $scope.$apply(function () {
                        $scope.buDan.cyrq = $filter('date')(d,'yyyy-MM-dd');
                    });
                }
            });
        };
        $scope.changejian=function(a){
            if(a.toString().length>=7){
                $ionicLoading.show({
                  noBackdrop: true,
                  template: "件数最大限制7位数",
                  duration: 1500
                });
            }
        };
        AddCydFactory.queryFkfs(sid,yh_id,user_id);
        AddCydFactory.queryShfs(sid,yh_id,user_id);

        $scope.$on('InitData',function(){
            $scope.fkfs=AddCydFactory.getFkfs();
            $scope.shfs=AddCydFactory.getShfs();
        });
        $(".zlDot").on("blur",function(){
          $(this).val(parseFloat($(this).val()).toFixed(3));
          $scope.buDan.zl=$(this).val();
          if($scope.buDan.zl>3000000){
            $ionicLoading.show({
              noBackdrop: true,
              template: "重量不可超过3000000吨",
              duration: 1500
            });
            $("#zl").css("color","red");
          }else{
            $("#zl").css("color","black");
          }
        });
        $(".tjDot").on("blur",function(){
          $(this).val(parseFloat($(this).val()).toFixed(3));
          $scope.buDan.tj=$(this).val();
          if($scope.buDan.tj>3000000){
            $ionicLoading.show({
              noBackdrop: true,
              template: "体积不可超过3000000立方米",
              duration: 1500
            });
            $("#tj").css("color","red");
          }else{
            $("#tj").css("color","black");
          }
        });
        $(".numDot").on("blur",function(){
          $(this).val(parseFloat($(this).val()).toFixed(2));
          $scope.buDan.yf=$(this).val();
          if($scope.buDan.yf>3000000){
            $ionicLoading.show({
              noBackdrop: true,
              template: "费用金额不可超过3000000元",
              duration: 1500
            });
            $("#yf").css("color","red");
          }
          else {
            $("#yf").css("color","black");
          }
        });
        $(".xfDot").on("blur",function(){
          $(this).val(parseFloat($(this).val()).toFixed(2));
          $scope.buDan.yf_xf=$(this).val();
          if($scope.buDan.yf_xf>3000000){
            $ionicLoading.show({
              noBackdrop: true,
              template: "费用金额不可超过3000000元",
              duration: 1500
            });
            $("#yfxf").css("color","red");
          }else{
            $("#yfxf").css("color","black");
          }
        });
        $(".tfDot").on("blur",function(){
          $(this).val(parseFloat($(this).val()).toFixed(2));
          $scope.buDan.yf_tf=$(this).val();
          if($scope.buDan.yf_tf>3000000){
            $ionicLoading.show({
              noBackdrop: true,
              template: "费用金额不可超过3000000元",
              duration: 1500
            });
            $("#yftf").css("color","red");
          }else{
            $("#yftf").css("color","black");
          }
        });
        $(".hfDot").on("blur",function(){
          $(this).val(parseFloat($(this).val()).toFixed(2));
          $scope.buDan.yf_hf=$(this).val();
          if($scope.buDan.yf_hf>3000000){
            $ionicLoading.show({
              noBackdrop: true,
              template: "费用金额不可超过3000000元",
              duration: 1500
            });
            $("#yfhf").css("color","red");
          }else{
            $("#yfhf").css("color","black");
          }
        });
        $(".yjDot").on("blur",function(){
          $(this).val(parseFloat($(this).val()).toFixed(2));
          $scope.buDan.yf_yj=$(this).val();
          if($scope.buDan.yf_yj>3000000){
            $ionicLoading.show({
              noBackdrop: true,
              template: "费用金额不可超过3000000元",
              duration: 1500
            });
            $("#yfyj").css("color","red");
          }else{
            $("#yfyj").css("color","black");
          }
        });
        $(".dkDot").on("blur",function(){
          $(this).val(parseFloat($(this).val()).toFixed(2));
          $scope.buDan.yf_ycdk=$(this).val();
          if($scope.buDan.yf_ycdk>3000000){
            $ionicLoading.show({
              noBackdrop: true,
              template: "费用金额不可超过3000000元",
              duration: 1500
            });
            $("#yfdk").css("color","red");
          }else{
            $("#yfdk").css("color","black");
          }
        });
        $(".dshkDot").on("blur",function(){
          $(this).val(parseFloat($(this).val()).toFixed(2));
          $scope.buDan.dshk=$(this).val();
          if($scope.buDan.dshk>3000000){
            $ionicLoading.show({
              noBackdrop: true,
              template: "费用金额不可超过3000000元",
              duration: 1500
            });
            $("#dskhf").css("color","red");
          }else{
            $("#dskhf").css("color","black");
          }
        });
        $(".xxfDot").on("blur",function(){
          $(this).val(parseFloat($(this).val()).toFixed(2));
          $scope.buDan.xxf_xf=$(this).val();
          if($scope.buDan.xxf_xf>3000000){
            $ionicLoading.show({
              noBackdrop: true,
              template: "费用金额不可超过3000000元",
              duration: 1500
            });
            $("#xf").css("color","red");
          }else{
            $("#xf").css("color","black");
          }
        });
        $(".qfDot").on("blur",function(){
          $(this).val(parseFloat($(this).val()).toFixed(2));
          $scope.buDan.xxf_qf=$(this).val();
          if($scope.buDan.xxf_qf>3000000){
            $ionicLoading.show({
              noBackdrop: true,
              template: "费用金额不可超过3000000元",
              duration: 1500
            });
            $("#qf").css("color","red");
          }else{
            $("#qf").css("color","black");
          }
        });
        $(".shfDot").on("blur",function(){
          $(this).val(parseFloat($(this).val()).toFixed(2));
          $scope.buDan.shf=$(this).val();
          if($scope.buDan.shf>3000000){
            $ionicLoading.show({
              noBackdrop: true,
              template: "费用金额不可超过3000000元",
              duration: 1500
            });
            $("#shf").css("color","red");
          }else{
            $("#shf").css("color","black");
          }
        });
        $(".thfDot").on("blur",function(){
          $(this).val(parseFloat($(this).val()).toFixed(2));
          $scope.buDan.thf=$(this).val();
          if($scope.buDan.thf>3000000){
            $ionicLoading.show({
              noBackdrop: true,
              template: "费用金额不可超过3000000元",
              duration: 1500
            });
            $("#thf").css("color","red");
          }else{
            $("#thf").css("color","black");
          }
        });
        $(".beDot").on("blur",function(){
          $(this).val(parseFloat($(this).val()).toFixed(2));
          $scope.buDan.bjsm=$(this).val();
          if($scope.buDan.bjsm>3000000){
            $ionicLoading.show({
              noBackdrop: true,
              template: "费用金额不可超过3000000元",
              duration: 1500
            });
            $("#be").css("color","red");
          }else{
            $("#be").css("color","black");
          }
        });
        $(".bfDot").on("blur",function(){
          $(this).val(parseFloat($(this).val()).toFixed(2));
          $scope.buDan.bf=$(this).val();
          if($scope.buDan.bf>3000000){
            $ionicLoading.show({
              noBackdrop: true,
              template: "费用金额不可超过3000000元",
              duration: 1500
            });
            $("#bf").css("color","red");
          }else{
            $("#bf").css("color","black");
          }
        });
        $(".yfdfDot").on("blur",function(){
          $(this).val(parseFloat($(this).val()).toFixed(2));
          $scope.buDan.yf_df=$(this).val();
          if($scope.buDan.yf_df>3000000){
            $ionicLoading.show({
              noBackdrop: true,
              template: "费用金额不可超过3000000元",
              duration: 1500
            });
            $("#yfdf").css("color","red");
          }else{
            $("#yfdf").css("color","black");
          }
        });
        $(".dskdfDot").on("blur",function(){
          $(this).val(parseFloat($(this).val()).toFixed(2));
          $scope.buDan.dsk_df=$(this).val();
          if($scope.buDan.dsk_df>3000000){
            $ionicLoading.show({
              noBackdrop: true,
              template: "费用金额不可超过3000000元",
              duration: 1500
            });
            $("#dskxf").css("color","red");
          }else{
            $("#dskxf").css("color","black");
          }
        });
        $scope.chooseShr=function(htmlbs){
            if( $scope.tbzt.isCk==true){
              $scope.buDan.tbzt='1';
            }else {
              $scope.buDan.tbzt='';
            }
            if( $scope.sfhd.isHd==true){
              $scope.buDan.sfhd='1';
            }else {
              $scope.buDan.sfhd='';
            }
            $timeout(function(){
                Storage.set('buDan',$scope.buDan);
            },300);
            if ($scope.imgDataB!=undefined){
              Storage.set('imgData',$scope.imgDataB);
            }else {
              Storage.set('imgData',null);
            }
            Storage.set('provincef',$('#provincef option:selected').val());
            Storage.set('cityf',$('#cityf option:selected').val());
            Storage.set('areaf',$('#areaf option:selected').val());
            Storage.set('provinces',$('#provinces option:selected').val());
            Storage.set('citys',$('#citys option:selected').val());
            Storage.set('areas',$('#areas option:selected').val());
            Storage.set('bdShfs',$('#shfs option:selected').val());
            Storage.set('hzFkfs',$('#fkType option:selected').val());
            $state.go('hzShrList',{htmlbs:htmlbs,fhd_id:id});
        };
        //$scope.items= [{index:1,name:'送货费'},{index:2,name:'提货费'}, {index:3,name:'保额'},{index:4,name:'保费'},{index:5,name:'运费垫付'},{index:6,name:'代收款垫付'}];
        var popupForWarehouse;
        $scope.addMenu=function(){
            popupForWarehouse = $ionicPopup.alert({
                template: '<div class="list">                '+
                '  <ion-item ng-repeat="item in items" ng-click="removefy($index,item.index)" class="item item-icon-right" id="item.index"> '+
                '    <h2 >{{item.name}}<h2>                   '+
                '  </ion-item>                                      '+
                '</div>                                      ',
                title:'请选择需要添加的费用',
                scope: $scope,
                okText:'取消'
            });
        };
        $scope.removefy= function (r,i) {
            if(i!=3&&i!=4){
                $scope.items.splice(r,1);
            }
            else if(i==3){
                $scope.items.splice(r,2);
            }else if(i==4){
                $scope.items.splice(r-1,2);
            }
            switch(i) {
                case 1:
                    $("#shfLab").removeClass("hide").addClass("showfy");
                    break;
                case 2:
                    $("#thfLab").removeClass("hide").addClass("showfy");
                    break;
                case 3:
                    $("#beLab").removeClass("hide").addClass("showfy");
                    $("#bfLab").removeClass("hide").addClass("showfy");
                    break;
                case 4:
                    $("#beLab").removeClass("hide").addClass("showfy");
                    $("#bfLab").removeClass("hide").addClass("showfy");
                    break;
                case 5:
                    $("#yfdfLab").removeClass("hide").addClass("showfy");
                    break;
                case 6:
                    $("#dskdfLab").removeClass("hide").addClass("showfy");
                    break;
                default:
                    break;
            }
            popupForWarehouse.close();
        };
        $scope.holdonRemove=function(i){
            var receipt = $ionicPopup.confirm({
                title : '是否删除此费用项',
                cancelText : '否',
                cancelType : 'button-stable',
                okText : '是'
            });
            receipt.then(function(r){
                if(r){
                    switch(i) {
                        case 1:
                            $("#shfLab").removeClass("showfy").addClass("hide");
                            $scope.items.splice(0,0,{index:1,name:'送货费'});
                            $scope.buDan.shf='';
                            break;
                        case 2:
                            $("#thfLab").removeClass("showfy").addClass("hide");
                            $scope.items.splice(1,0,{index:2,name:'提货费'});
                            $scope.buDan.thf='';
                            break;
                        case 3:
                            $("#beLab").removeClass("showfy").addClass("hide");
                            $("#bfLab").removeClass("showfy").addClass("hide");
                            $scope.items.splice(2,0,{index:3,name:'保额'});
                            $scope.items.splice(3,0,{index:4,name:'保费'});
                            $scope.buDan.bjsm='';
                            $scope.buDan.bf='';
                            $scope.tbzt.isCk=false;
                            break;
                        case 4:
                            $("#beLab").removeClass("showfy").addClass("hide");
                            $("#bfLab").removeClass("showfy").addClass("hide");
                            $scope.items.splice(2,0,{index:3,name:'保额'});
                            $scope.items.splice(3,0,{index:4,name:'保费'});
                            $scope.buDan.bjsm='';
                            $scope.buDan.bf='';
                            $scope.tbzt.isCk=false;
                            break;
                        case 5:
                            $("#yfdfLab").removeClass("showfy").addClass("hide");
                            $scope.items.splice(4,0,{index:5,name:'运费垫付'});
                            $scope.buDan.yf_df='';
                            break;
                        case 6:
                            $("#dskdfLab").removeClass("showfy").addClass("hide");
                            $scope.items.splice(5,0,{index:6,name:'代收款垫付'});
                            $scope.buDan.dsk_df='';
                            break;
                        default:
                            break;
                    }
                }else{
                    console.log('取消');
                }
            });
        };
        if (Storage.get('provincesData')!=null){
          $scope.provinces=Storage.get('provincesData');
        }else {
          FProCitAreFactory.getPro(sid,user_id,yh_id);
        }
        $scope.$on('FPro.proUpdated',function(){
            $scope.provinces=FProCitAreFactory.getP();
            Storage.set('provincesData',$scope.provinces);
        });
        $scope.$on('FCity.cityUpdated',function(){
            $scope.citiesF=FProCitAreFactory.getC();
            if (changeDqBsF==true){
                FProCitAreFactory.getAre(sid,user_id,yh_id,$scope.citiesF[0].code);
            }
            Storage.set('citiesDataf',$scope.citiesF);
        });
        $scope.$on('FArea.areaUpdated',function(){
            $scope.areasF=FProCitAreFactory.getA();
            Storage.set('areasDataf',$scope.areasF);
        });
        $scope.$on('SCity.cityUpdated',function(){
            $scope.citiesS=SProCitAreFactory.getC();
            if (changeDqBsS==true){
                SProCitAreFactory.getAre(sid,user_id,yh_id,$scope.citiesS[0].code);
            }
            Storage.set('citiesDatas',$scope.citiesS);
        });
        $scope.$on('SArea.areaUpdated',function(){
            $scope.areasS=SProCitAreFactory.getA();
            Storage.set('areasDatas',$scope.areasS);
        });
        $("#provincef").change(function(e) {
            changeDqBsF=true;
            FProCitAreFactory.getCity(sid,user_id,yh_id,$('#provincef').val());
            if ($('#provincef').val()=='地区选择'){
                $timeout(function(){
                    $scope.citiesF=null;
                    $scope.areasF=null;
                },300);
            }
        });
        $("#cityf").change(function(e) {
            FProCitAreFactory.getAre(sid,user_id,yh_id,$('#cityf').val());
        });
        $("#provinces").change(function(e) {
            changeDqBsS=true;
            SProCitAreFactory.getCity(sid,user_id,yh_id,$('#provinces').val());
            if ($('#provinces').val()=='地区选择'){
                $timeout(function(){
                    $scope.citiesS=null;
                    $scope.areasS=null;
                },300);
            }
        });
        $("#citys").change(function(e) {
            SProCitAreFactory.getAre(sid,user_id,yh_id,$('#citys').val());
        });
        HzCydDetailFactory.getImgData(id,sid,user_id,yh_id);
        $scope.$on('Image.detailUpdated',function(){
            var imgData= HzCydDetailFactory.getImg();
            $scope.imgDataF = imgData;
        });
        $scope.actionSheet=function(){
            var imgPic=[];
            var hideSheet = $ionicActionSheet.show({
                buttons: [
                    { text: '拍照' },
                    { text: '从相册选择' }
                ],
                cancelText: '取消',
                cancel: function() {
                    // add cancel code..
                },
                buttonClicked: function(index) {
                    if(index==0){
                        var options = {
                            destinationType: Camera.DestinationType.NATIVE_URI,
                            sourceType: Camera.PictureSourceType.CAMERA,
                            targetWidth: 800,
                            targetHeight: 800,
                        };

                        $cordovaCamera.getPicture(options).then(function(imageURI) {
                            if (imgPic.length+$scope.imgDataF.length>2){
                                $ionicLoading.show({
                                    noBackdrop: true,
                                    template: "最多支持三张照片",
                                    duration: 1500
                                });
                            }else {
                                imgPic[imgPic.length]= imageURI;
                            }
                        }, function(err) {
                            // error
                        });
                    }else if (index==1){
                        var options = {
                            maximumImagesCount: 10,
                            width: 800,
                            height: 800,
                            quality: 80
                        };

                        $cordovaImagePicker.getPictures(options)
                            .then(function (results) {
                                if (imgPic.length+results.length+$scope.imgDataF.length>3){
                                    $ionicLoading.show({
                                        noBackdrop: true,
                                        template: "最多支持三张照片",
                                        duration: 1500
                                    });
                                }else {
                                    for (var i = 0,j=imgPic.length; i < results.length; i++,j++) {
                                        imgPic[j]=results[i];
                                    }
                                }
                            }, function(error) {
                                // error getting photos
                            });
                    }
                    $scope.imgDataB=imgPic;
                    return true;
                }
            });
        };
        var deleteImgData=[];
        $scope.deleteImgF=function(id){
            var j;
            var receipt = $ionicPopup.confirm({
                title : '是否删除图片',
                cancelText : '否',
                cancelType : 'button-stable',
                okText : '是'
            });
            receipt.then(function(r){
                if(r){
                    for (var i=0;i<$scope.imgDataF.length;i++){
                        if ($scope.imgDataF[i].image_id==id){
                            j=i;
                            $scope.imgDataF.splice(j,1);
                        }
                    }
                    deleteImgData[deleteImgData.length]=id;
                }else{
                    console.log('取消');
                }
            });
        };
        $scope.deleteImgB=function(img){
            var j;
            var receipt = $ionicPopup.confirm({
                title : '是否删除图片',
                cancelText : '否',
                cancelType : 'button-stable',
                okText : '是'
            });
            receipt.then(function(r){
                if(r){
                    for (var i=0;i<$scope.imgDataB.length;i++){
                        if ($scope.imgDataB[i]==img){
                            j=i;
                            $scope.imgDataB.splice(j,1);
                        }
                    }
                }else{
                    console.log('取消');
                }
            });
        };
        //保存修改
        $scope.saveCyd=function(){
            if( $scope.tbzt.isCk==true){
                $scope.buDan.tbzt='1';
            }else {
                $scope.buDan.tbzt='';
            }
            if( $scope.sfhd.isHd==true){
                $scope.buDan.sfhd='1';
            }else {
                $scope.buDan.sfhd='';
            }
            var fproCode=$('#provincef').val();
            var fcityCode=$('#cityf').val();
            var fareaCode=$('#areaf').val();
            var sproCode=$('#provinces').val();
            var scityCode=$('#citys').val();
            var sareaCode=$('#areas').val();
            var fpro=$('#provincef option:selected').text();
            var fcity=$('#cityf option:selected').text();
            var farea=$('#areaf option:selected').text();
            var spro=$('#provinces option:selected').text();
            var scity=$('#citys option:selected').text();
            var sarea=$('#areas option:selected').text();
            var shfs=$('#shType option:selected').val();
            var fhrCity=$('#cityF option:selected').text();
            var shfs=$('#shfs option:selected').val();
            $("#saveBd").attr("disabled", true);
            Storage.set('buDan',null);
            if ($('#provincef option:selected').text()=='地区选择'){
              fproCode=0;
              fcityCode=0;
              fareaCode=0;
            }
            if ($('#provinces option:selected').text()=='地区选择'){
              sproCode=0;
              scityCode=0;
              sareaCode=0;
            }
            if ($scope.buDan.sl==''){
              $scope.buDan.sl=0;
            }
            if ($scope.buDan.cydh==''){
                $ionicLoading.show({
                    noBackdrop: true,
                    template: "请输入承运单号",
                    duration: 600
                });
                $("#saveBd").attr("disabled", false);
                $timeout(function(){
                    $("#cydhCode").focus();
                },160);
            } else if ($scope.buDan.dwmc==''){
                $ionicLoading.show({
                    noBackdrop: true,
                    template: "请输入物流公司",
                    duration: 600
                });
                $("#saveBd").attr("disabled", false);
                $timeout(function(){
                    $("#dwmc").focus();
                },160);
            }else {
                saveBdFactory.checkCydhExist(sid,user_id,yh_id,$scope.buDan.cydh,$scope.buDan.dwmc,id);
                $scope.$on('Cydh.check',function(){
                  var result=saveBdFactory.getCount();
                  if (result==0){
                    changeBdFactory.getChangeBd(id,sid,user_id,yh_id,$scope.buDan,fpro,fcity,farea,spro,scity,sarea,fproCode,fcityCode,fareaCode,sproCode,scityCode,sareaCode,shfs);
                  }else {
                    $ionicLoading.show({
                      noBackdrop: true,
                      template: "承运单号已存在！",
                      duration: 1500
                    });
                    $("#saveBd").attr("disabled", false);
                    $timeout(function(){
                      $("#cydhCode").focus();
                    },160);
                  }
                });
                $scope.$on('Change.bdUpdated',function(){
                    var result=changeBdFactory.getResult();
                    if (result==1){
                        $ionicLoading.show({
                            noBackdrop: true,
                            template: "修改成功",
                            duration: 600
                        });
                        $("#saveBd").attr("disabled", false);
                        $state.go('hzcydlist');
                        $timeout(function(){
                            uploadImg(id);
                            deleteImg();
                        },160);
                    }
                });
            }
        };
        function uploadImg(fhd_id){
            if ($scope.imgDataB!=undefined){
                for (var i=0;i<$scope.imgDataB.length;i++){
                    var canvas = document.createElement('CANVAS');
                    var ctx = canvas.getContext('2d');
                    var dataURL;
                    var img = new Image();
                    img.crossOrigin = 'Anonymous';
                    img.src = $scope.imgDataB[i];
                    img.onload = function(){
                        canvas.height = this.height;
                        canvas.width = this.width;
                        ctx.drawImage(this,0,0,this.width,this.height);
                        var ext = this.src.substring(this.src.lastIndexOf(".")+1).toLowerCase();
                        var extendName = this.src.substring(this.src.lastIndexOf("/")+1).toLowerCase();
                        dataURL = canvas.toDataURL("image/"+ext);
                        saveBdFactory.getSavaPic(sid,user_id,yh_id,dataURL,fhd_id,ext,extendName);
                    };
                }
            }
            $scope.$on('Save.picUpdated',function(){
                var result=saveBdFactory.getSavePicResult();
                if (result==1){

                }
            });
        }
        function deleteImg(){
            for (var i=0;i<deleteImgData.length;i++){
                saveBdFactory.getDeletePic(sid,user_id,yh_id,deleteImgData[i]);
            }

            $scope.$on('Delete.picUpdated',function(){
                var result=saveBdFactory.getDeletePicResult();
                if (result==1){

                }
            });
        }
    })
    .controller('HzCydDetailCtrl',function($scope,$stateParams,HzCydDetailFactory,HzCydProcessFactory,$cordovaGeolocation,$ionicHistory,Storage,$window,$ionicPopup,CydDetailFactory,CydProcessFactory){
        var fhd_id = $stateParams['fhd_id'];
        var cyd_id=$stateParams['cyd_id'];
        var sid=Storage.get('sid');
        var user_id=Storage.get('user_id');
        var yh_id=Storage.get('yh_id');
        $scope.showloading=true;
        if (fhd_id!=''){
            HzCydDetailFactory.getDetail(fhd_id,sid,user_id,yh_id);
            HzCydDetailFactory.getImgData(fhd_id,sid,user_id,yh_id);
            HzCydProcessFactory.getProcessfhd(fhd_id,sid,user_id,yh_id);
        }else {
            $scope.showloading=false;
            CydDetailFactory.getDetail(cyd_id,sid,user_id,yh_id);
            CydProcessFactory.getProcess(cyd_id,sid,user_id,yh_id);
        }
        $scope.$on('FhdDetail.detailUpdated',function(){
           var fhdDetailData= HzCydDetailFactory.getDetailContent();
            console.log(fhdDetailData);
            switch (fhdDetailData.shfs){
                case 1 :
                    $scope.shfs = '自提';
                    break;
                case 2 :
                    $scope.shfs = '送货';
                    break;
                case 3 :
                    $scope.shfs = '上楼';
                    break;
                default :
                    break;
            }
            $scope.fhdDetailData = fhdDetailData;
        });
        $scope.$on('CydDetail.detailUpdated',function(){
            var cydDetailData= CydDetailFactory.getDetailContent();
            console.log(cydDetailData);
            switch (cydDetailData.shfs){
                case 1 :
                    $scope.shfs = '自提';
                    break;
                case 2 :
                    $scope.shfs = '送货';
                    break;
                case 3 :
                    $scope.shfs = '上楼';
                    break;
                default :
                    break;
            }
            $scope.fhdDetailData = cydDetailData;
        });
        $scope.$on('Image.detailUpdated',function(){
            var imgData= HzCydDetailFactory.getImg();
            console.log(imgData);
            $scope.imgData = imgData;
            $scope.showloading=false;
        });
        $scope.$on('FhdProcess.processUpdated',function(){
            var fhdProcessData = HzCydProcessFactory.getfhd();
            $scope.processFirstfhd=fhdProcessData[0];
            var processData=[];
            var j;
            for(var i=1;i<fhdProcessData.length;i++){
                j=i-1;
                processData[j]=fhdProcessData[i];
            }
            $scope.ProcessDatafhd = processData;
        });
        $scope.$on('CydProcess.processUpdated',function(){
            var cydProcessData = CydProcessFactory.getProcessContent();
            $scope.processFirstfhd=cydProcessData[0];
            var processData=[];
            var j;
            for(var i=1;i<cydProcessData.length;i++){
                j=i-1;
                processData[j]=cydProcessData[i];
            }
            $scope.ProcessDatafhd=processData;
        });
        //拨号
        $scope.callPhone = function (mobilePhone) {
            var confirmPopup = $ionicPopup.confirm({
                title:mobilePhone,
                okText:'呼叫',
                cancelText:'取消'
            });
            confirmPopup.then(function(res) {
                if (res) {
                    $window.location.href = "tel:" + mobilePhone;
                }else {

                }
            });
        };
        //点击图片放大
        $scope.shouBigImage = function (imageName) {  //传递一个参数（图片的URl）
            $scope.Url = imageName;                   //$scope定义一个变量Url，这里会在大图出现后再次点击隐藏大图使用
            $scope.bigImage = true;                   //显示大图
        };
        $scope.bigImage = false;    //初始默认大图是隐藏的
        $scope.hideBigImage = function () {
            $scope.bigImage = false;
        };
        $scope.myGoBack = function() {
            $ionicHistory.goBack();
        };
    })
    .controller('HzCydSearchCtrl',function($scope,$cordovaDatePicker,$state,$filter){
        var t = new Date();
        var iToDay=t.getDate();
        var iToMon=t.getMonth();
        var iToYear=t.getFullYear();
        var iTDate=new Date(iToYear,iToMon,(iToDay-30));
        $scope.cyd={
            cydh:'',
            shrdh:'',
            shrmc:''
        };
        $scope.startDate=$filter('date')(iTDate,'yyyy-MM-dd');
        $scope.showStartDate = function() { //选择日期
            var optionsStart = {
              date: iTDate,
              mode: 'date',
              maxDate: new Date($scope.endDate) - 10000
            };
            datePicker.show(optionsStart, function(d) {
                if (!isNaN(d.getTime())) {  // valid date
                    $scope.$apply(function () {
                        iTDate=d;
                        $scope.startDate = $filter('date')(iTDate,'yyyy-MM-dd');
                    });
                }
            });
        };

        var endTime = new Date();
        $scope.endDate=$filter('date')(endTime,'yyyy-MM-dd');
        $scope.showEndDate = function(){
            console.log('EndDate');
            var optionsEnd = {
                date : endTime,
                mode : 'date',
                minDate: new Date($scope.startDate) - 10000
            };
            datePicker.show(optionsEnd,function(d) {
                if (!isNaN(d.getTime())) {  // valid date
                    $scope.$apply(function () {
                        endTime=d;
                        $scope.endDate = $filter('date')(endTime,'yyyy-MM-dd');
                    });
                }
            });

        };

        $scope.searchCyd = function(cyd){
            var fhdzt=$("#fhdzt option:selected").val();
            $state.go('hzcydlist',{cydh:cyd.cydh,kssj:$scope.startDate,jssj:$scope.endDate,shrdh:cyd.shrdh,shrmc:cyd.shrmc,fhdzt:fhdzt});
        };
    });


